﻿/**************************************************************
 *
 * 唯一标识：3fe0a277-ae79-4b6b-b2fd-ed182906e177
 * 命名空间：Sgr.UPMS.Application.Validations
 * 创建时间：2023/8/24 19:28:38
 * 机器名称：DESKTOP-S0D075D
 * 创建者：antho
 * 电子邮箱：fengqinhua2016@163.com
 * 描述：
 *
 **************************************************************/

using FluentValidation;
using Sgr.UPMS.Application.Commands.Organizations;
using Sgr.Utilities;

namespace Sgr.UPMS.Application.Validations.Organizations
{
    /// <summary>
    /// 更新组织机构命令验证器
    /// </summary>
    public class UpdateOrgCommandValidator : AbstractValidator<UpdateOrgCommand>
    {
        public UpdateOrgCommandValidator()
        {
            RuleFor(command => command.Id)
                .GreaterThan(0)
                .WithMessage("组织机构ID必须大于0");

            RuleFor(command => command.Name)
                .NotEmpty()
                .WithMessage("组织机构名称不能为空")
                .MaximumLength(100)
                .WithMessage("组织机构名称不能超过100个字符");

            RuleFor(command => command.OrgTypeCode)
                .MaximumLength(50)
                .WithMessage("组织机构类型编码不能超过50个字符");

            RuleFor(command => command.AreaCode)
                .MaximumLength(50)
                .WithMessage("行政区划编码不能超过50个字符");

            RuleFor(command => command.StaffSizeCode)
                .MaximumLength(50)
                .WithMessage("人员规模编码不能超过50个字符");

            RuleFor(command => command.Remarks)
                .MaximumLength(500)
                .WithMessage("描述不能超过500个字符");

            RuleFor(command => command.Leader)
                .MaximumLength(20)
                .WithMessage("负责人名称不能超过20个字符");

            // Phone验证
            RuleFor(command => command.Phone)
                .MaximumLength(30)
                .WithMessage("联系电话不能超过30个字符");

            When(command => !string.IsNullOrWhiteSpace(command.Phone), () =>
            {
                RuleFor(command => command.Phone)
                    .Must(phone => phone != null && RegexHelper.IsValidPhoneNumber(phone))
                    .WithMessage("无效的手机号码格式");
            });

            // Email验证
            RuleFor(command => command.Email)
                .MaximumLength(30)
                .WithMessage("联系邮箱不能超过30个字符");

            When(command => !string.IsNullOrWhiteSpace(command.Email), () =>
            {
                RuleFor(command => command.Email)
                    .Must(email => email != null && RegexHelper.IsValidEmail(email))
                    .WithMessage("请输入有效的邮箱地址");
            });

            RuleFor(command => command.Address)
                .MaximumLength(100)
                .WithMessage("地址不能超过100个字符");
        }
    }
}